package org.terraz.lcs {
	import flash.events.MouseEvent;	
	import flash.display.Sprite;
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;
	
	/**
	 * @author TarGz
	 */
	public class Slave extends Client{
		private const type : String  = "slave";
		private var id : String;
		public function Bt():void{
		}
		

		override public function init():void{
			Lcs.getLcs().log("Slave.init()");
			id = "slave"+ Math.round((Math.random()*9 ) * 1000);
			Lcs.getLcs().log("Slave.slave()"+id);	
			bouton = new Bouton();
			bouton.draw(300,0,50,50,0xFFFFFF);
			bouton.addEventListener(MouseEvent.MOUSE_UP, onClick);
			this.addChild(bouton);
			
			connect();
		}
		

		// PRIVATE
		private function connect():void{
			slaveConnection = new LocalConnection();
			slaveConnection.client = this;
			masterConnection = new LocalConnection();
			masterConnection.addEventListener(StatusEvent.STATUS, onMasterStatus);
			
			try {
				slaveConnection.connect(id);
			} catch (error:ArgumentError) {
                Lcs.getLcs().log("Can't connect to "+id+"...the connection name is already being used by another SWF");
            }
			masterConnection.send(ConnectionID.master,"onSlaveConnection", id);
		}
		
		 private function onMasterStatus(event:StatusEvent):void {
        	Lcs.getLcs().log("Slave.onMasterStatus(event)");
            switch (event.level) {
                case "status":
                    Lcs.getLcs().log("Master.send() succeeded");
                    break;
                case "error":
                     Lcs.getLcs().log("Master.send() failed");
                    break;
            }
        }
        private function onClick(e : MouseEvent) : void {
        	Lcs.getLcs().log("Slave.onClick(e)");
        	masterConnection.send(ConnectionID.master,"onSlaveMessage", "Hello master i'm Slave "+id);
        }
        public function onMasterMessage(m:String):void{
			Lcs.getLcs().log("Slave.onMasterMessage("+m+")");
		}
		private var slaveConnection : LocalConnection;
		private var masterConnection : LocalConnection;
		private var bouton : Bouton;
	}
}
